
package rentalgui;

import database.ExpenseDao;
import database.PropertyDao;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JOptionPane;
import rentaldb.Expense;
import rentaldb.Property;

public class ExpenseUpdateDeleteWindow extends javax.swing.JFrame {

    /**
     * Creates new form ExpenseUpdateDeleteWindow
     */
    public ExpenseUpdateDeleteWindow(ExpenseDao edao, PropertyDao pdao) {
        initComponents();
        this.edao = edao;
        this.pdao = pdao;
        updateMode = false;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        inputPropertyId = new javax.swing.JTextField();
        inputType = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        inputExpenseId = new javax.swing.JTextField();
        inputDueDate = new javax.swing.JTextField();
        inputAmount = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        buttonSearch = new javax.swing.JButton();
        buttonDelete = new javax.swing.JButton();
        buttonUpdate = new javax.swing.JButton();
        buttonExit = new javax.swing.JButton();
        inputBox = new javax.swing.JTextField();
        buttonClear = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Expense Update/Delete");

        inputPropertyId.setEditable(false);

        inputType.setEditable(false);

        jLabel3.setText("Due Date");

        jLabel2.setText("Expense ID");

        jLabel5.setText("Property ID");

        jLabel4.setText("Amount");

        jLabel6.setText("Type");

        inputExpenseId.setEditable(false);

        inputDueDate.setEditable(false);

        inputAmount.setEditable(false);

        jLabel7.setText("Expense ID");

        buttonSearch.setText("Search");
        buttonSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonSearchActionPerformed(evt);
            }
        });

        buttonDelete.setText("Delete");
        buttonDelete.setEnabled(false);
        buttonDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonDeleteActionPerformed(evt);
            }
        });

        buttonUpdate.setText("Update");
        buttonUpdate.setEnabled(false);
        buttonUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonUpdateActionPerformed(evt);
            }
        });

        buttonExit.setText("Exit");
        buttonExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonExitActionPerformed(evt);
            }
        });

        buttonClear.setText("Clear");
        buttonClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonClearActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(inputAmount, javax.swing.GroupLayout.DEFAULT_SIZE, 270, Short.MAX_VALUE)
                                    .addComponent(inputPropertyId)))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(jLabel2)
                                        .addComponent(jLabel6)
                                        .addComponent(jLabel3))
                                    .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.TRAILING))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(inputType)
                                    .addComponent(inputExpenseId)
                                    .addComponent(inputDueDate)
                                    .addComponent(inputBox)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(buttonSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(buttonClear, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(buttonDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(buttonUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(buttonExit, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(26, 26, 26))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(inputBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonSearch)
                    .addComponent(buttonClear))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(inputExpenseId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(inputType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(inputDueDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(inputAmount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(inputPropertyId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonExit)
                    .addComponent(buttonUpdate)
                    .addComponent(buttonDelete))
                .addGap(72, 72, 72))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void buttonClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonClearActionPerformed
        clearTextBoxes();
        setEditableStatus(false);
        updateMode = false;
        buttonUpdate.setEnabled(false);
        buttonDelete.setEnabled(false);
    }//GEN-LAST:event_buttonClearActionPerformed

    private void buttonSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSearchActionPerformed
        try{
            Integer input = Integer.parseInt(inputBox.getText().trim());
            curExp = edao.getExpenseById(input);
            putCurrentInTextBoxes();
            setEditableStatus(true);
            updateMode = false;
            buttonUpdate.setEnabled(true);
            buttonDelete.setEnabled(true);
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(null, "Invalid ID.", "ERROR", JOptionPane.ERROR_MESSAGE);
        }        
    }//GEN-LAST:event_buttonSearchActionPerformed

    private void buttonDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonDeleteActionPerformed
        int input = JOptionPane.showConfirmDialog(null, "Are you sure you want to delete this entry?", "Delete?", JOptionPane.YES_NO_OPTION);
        if(input == JOptionPane.YES_OPTION){
            try{
                edao.delete(curExp);
                buttonClearActionPerformed(evt);
            }
            catch(Exception e){
                JOptionPane.showMessageDialog(null, "Error during delete.", "Delete Error", JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_buttonDeleteActionPerformed

    private void buttonUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonUpdateActionPerformed
        if(!updateMode){
            updateMode = true;
            JOptionPane.showMessageDialog(null, "Make the desired changes to the fields, then click update again.", "Update Mode", JOptionPane.INFORMATION_MESSAGE);
            buttonDelete.setEnabled(false);
            setEditableStatus(true);
        }else{
            updateMode = false;
            buttonDelete.setEnabled(true);
            int input = JOptionPane.showConfirmDialog(null, "Are you sure you want to update this entry?", "Update?", JOptionPane.YES_NO_OPTION);
            if(input == JOptionPane.YES_OPTION){
                try{
                    Integer expId = Integer.parseInt(inputExpenseId.getText().trim());
                    String type = inputType.getText().trim();
                    DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
                    Date date = df.parse(inputDueDate.getText().trim());
                    Double amount = Double.parseDouble(inputAmount.getText().trim());
                    Integer propId = Integer.parseInt(inputPropertyId.getText().trim());
                    Expense exp = new Expense();
                    exp.setExpenseId(expId);
                    exp.setType(type);
                    exp.setDuedate(date);
                    exp.setAmount(amount);
                    Property prop = pdao.getPropertyById(propId);
                    exp.setPropertyid(prop);
                    edao.update(exp);
                }
                catch(Exception e){
                    JOptionPane.showMessageDialog(null, "Error during update.", "Update Error", JOptionPane.ERROR_MESSAGE);
                }
            } else{
                JOptionPane.showMessageDialog(this, "Changes Reset", "Update Cancelled", JOptionPane.INFORMATION_MESSAGE);
                putCurrentInTextBoxes();
            }
            buttonDelete.setEnabled(false);
            setEditableStatus(false);
        }
    }//GEN-LAST:event_buttonUpdateActionPerformed

    private void buttonExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonExitActionPerformed
        dispose();
    }//GEN-LAST:event_buttonExitActionPerformed

    private void setEditableStatus(boolean setting){
        inputBox.setEditable(setting);
        inputType.setEditable(setting);
        inputDueDate.setEditable(setting);
        inputAmount.setEditable(setting);
        inputPropertyId.setEditable(setting);
    }
    
    private void clearTextBoxes(){
        inputBox.setText("");
        inputExpenseId.setText("");
        inputType.setText("");
        inputDueDate.setText("");
        inputAmount.setText("");
        inputPropertyId.setText("");
    }
    
    private void putCurrentInTextBoxes(){
        inputExpenseId.setText(curExp.getExpenseId().toString().trim());
        inputType.setText(curExp.getType().toString().trim());
        inputDueDate.setText(curExp.getDuedate().toString().trim());
        inputAmount.setText(curExp.getAmount().toString().trim());
        inputPropertyId.setText(curExp.getPropertyid().toString().trim());
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonClear;
    private javax.swing.JButton buttonDelete;
    private javax.swing.JButton buttonExit;
    private javax.swing.JButton buttonSearch;
    private javax.swing.JButton buttonUpdate;
    private javax.swing.JTextField inputAmount;
    private javax.swing.JTextField inputBox;
    private javax.swing.JTextField inputDueDate;
    private javax.swing.JTextField inputExpenseId;
    private javax.swing.JTextField inputPropertyId;
    private javax.swing.JTextField inputType;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    // End of variables declaration//GEN-END:variables
    private Expense curExp;
    private ExpenseDao edao;
    private PropertyDao pdao;
    private boolean updateMode;
}
